package com.xzmzhp.controller.activity;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.activity.Activity;
import com.xzmzhp.service.activity.ActivityService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-04 09:58
 * @program: ams
 * @description: 活动管理前端交互类
 */

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 查询活动全部数据
     * @return 活动信息
     */
    @RequestMapping("/findAll")
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "10") int limit){
        //开启分页
        PageHelper.startPage(page, limit, true);
        List<Activity> list = activityService.findAll();
        if (CollUtil.isNotEmpty(list)){
            PageInfo<Activity> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询活动信息失败,请重新查询");
    }

    /**
     * 根据相应的参数查询数据
     * @param param 参数
     *        societyId 社团id
     *        activityName 活动名称
     *        startTime 开始时间
     *        endTime  结束时间
     * @return 活动信息
     */
    @RequestMapping("/findByParam")
    public ReturnData findByParam(@RequestParam(value = "page", defaultValue = "1") int page,
                                  @RequestParam(value = "limit", defaultValue = "10") int limit,
                                  @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<Activity> list = activityService.findByParam(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<Activity> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询活动信息失败,请重新查询");
    }

    /**
     * 获取最新的活动信息
     * @return 活动信息
     */
    @RequestMapping("/findLatestActivities")
    public ReturnData findLatestActivities(){
        Activity activity = activityService.findLatestActivities();
        if (StringUtils.isNotNull(activity)) {
            return ReturnData.ok(activity);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询活动信息失败,请重新查询");
        }
    }

    /**
     * 根据id查询活动信息
     * @param activityId 活动id
     * @return 活动信息
     */
    @RequestMapping("/findActivityById")
    public ReturnData findActivityById(@RequestParam(value = "activityId") Integer activityId){
        Activity activity = activityService.findActivityById(activityId);
        if (StringUtils.isNotNull(activity)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询活动信息失败,请重新查询");
        }
    }

    /**
     * 添加活动信息
     * @param activity 活动信息
     * @return 是否成功
     */
    @RequestMapping("/addActivity")
    public ReturnData addActivity(@RequestBody Activity activity){
        Integer i = activityService.addActivity(activity);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动信息添加错误");
        }
    }

    /**
     * 修改活动信息
     * @param activity 活动信息
     * @return 是否成功
     */
    @RequestMapping("/exitActivity")
    public ReturnData exitActivity(@RequestBody Activity activity){
        Integer i = activityService.exitActivity(activity);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动信息修改错误");
        }
    }

    /**
     * 根据活动id删除节目类型信息
     * @param activityId 活动id
     * @return 是否成功
     */
    @RequestMapping("/delActivity")
    public ReturnData delActivity(@RequestParam(value = "activityId") Integer activityId){
        Integer i = activityService.delActivity(activityId);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动信息删除错误");
        }
    }

    /**
     * 添加活动人员信息
     * @param activityId 活动id
     * @param personnelId 人员id
     * @return 是否成功
     */
    @RequestMapping("/addActivityPersonnel")
    public ReturnData addActivityPersonnel(@RequestParam(value = "activityId") Integer activityId,
                                           @RequestParam(value = "personnelId") Integer personnelId){
        Integer i = activityService.addActivityPersonnel(activityId,personnelId);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动人员信息添加错误");
        }
    }

    /**
     * 批量添加活动人员信息
     * @param activityId 活动id
     * @param personnelId 人员id集合
     * @return 是否成功
     */
    @RequestMapping("/addBatchActivityPersonnel")
    public ReturnData addBatchActivityPersonnel(@RequestParam(value = "activityId") Integer activityId,
                                           @RequestParam(value = "personnelId") String personnelId){
        Integer i = activityService.addActivityPersonnel(activityId,StringUtils.convert(personnelId));
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动人员信息添加错误");
        }
    }

    /**
     * 删除活动人员信息
     * @param activityId 活动id
     * @param personnelId 活动人员id
     * @return 是否成功
     */
    @RequestMapping("/delActivityPersonnel")
    public ReturnData delActivityPersonnel(@RequestParam(value = "activityId") Integer activityId,
                                           @RequestParam(value = "personnelId") Integer personnelId){
        Integer i = activityService.delActivityPersonnel(activityId,personnelId);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动人员信息删除错误");
        }
    }

    /**
     * 删除活动人员信息
     * @param activityId 活动id
     * @param personnelId 活动人员id集合
     * @return 是否成功
     */
    @RequestMapping("/delBatchActivityPersonnel")
    public ReturnData delBatchActivityPersonnel(@RequestParam(value = "activityId") Integer activityId,
                                           @RequestParam(value = "personnelId") String personnelId){
        Integer i = activityService.delActivityPersonnel(activityId,StringUtils.convert(personnelId));
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动人员信息添加错误");
        }
    }
}
